<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Applicants extends CI_Controller {

    var $Array_Data;
    var $CI;
    
    public function __construct() {
        parent::__construct();
        $this->CI =& get_instance();
        
        if(update_user() != 2){
            redirect(site_url() . '/welcome/not_authorized');
        };   
        $this->Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
    }
    
    public function Index(){
        $CI = $this->CI;
        $CI->load->model('Users3_model','Us3');
        $CI->load->model('Users2_FavoriteApplicants_model','Us2Fav');
        /*----- Get Data from Applicants -------*/
        $Applicants = $CI->Us3->Get_Advanced('*',array(),'UserFullname',30,0);
        $temp = array();
        foreach($Applicants as $App){
            $Check = $CI->Us2Fav->Get(array(
                'RelUserGroup2ID'   =>  $this->Array_Data['UserInfo']['UserGroup2ID'],
                'RelUserGroup3ID'   => $App['UserGroup3ID']   
            ));
            if(!empty($Check))
                $App['IsFavorite'] = true;
            else
                $App['IsFavorite'] = false;
            $temp[] = $App;
        }
        
        $this->Array_Data['Applicants'] = $temp;
        $this->load->view('user_group2/applicants/index_view',$this->Array_Data);
    }
    
    public function add_favorite(){
        $CI = $this->CI;
        $CI->load->model('Users2_FavoriteApplicants_model','Us2Fav');
        $data = array(
            'RelUserGroup2ID'  =>  $this->Array_Data['UserInfo']['UserGroup2ID'],
            'RelUserGroup3ID'  =>  $_POST['UserGroup3ID'],
        );
        $CI->Us2Fav->Insert($data);
        echo 1;
    }
    
    public function remove_favorite(){
        $CI = $this->CI;
        $CI->load->model('Users2_FavoriteApplicants_model','Us2Fav');
        $data = array(
            'RelUserGroup2ID'  =>  $this->Array_Data['UserInfo']['UserGroup2ID'],
            'RelUserGroup3ID'  =>  $_POST['UserGroup3ID'],
        );
        $CI->Us2Fav->Delete($data);
        echo 1;
    }
}